package com.tanhua.autoconfig.template;

import com.tanhua.autoconfig.properties.ApiFaceProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * @Author Pan Rundong
 * @Date 2023-02-25 14:07
 * @Desc 百度人脸识别装配类
 */
public class ApiFaceTemplate {
    @Autowired
    private ApiFaceProperties apiFaceProperties;

    public ApiFaceTemplate(ApiFaceProperties apiFaceProperties) {
        this.apiFaceProperties = apiFaceProperties;
    }

    /**
     * 人脸识别
     * @Author: Pan Rundong
     * @Date: 2023-02-25 14:12
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param image
     * @return boolean
     */
    public boolean detect(String image) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        String imageType = "URL";

        // 人脸检测
        JSONObject res = apiFaceProperties.apiFace().detect(image, imageType, options);

        Integer error_code = (Integer) res.get("error_code");

        return error_code == 0;
    }
}
